import java.util.*;

public class Solution599 {
    public String[] findRestaurant(String[] list1, String[] list2) {
        Map<String,Integer> map=new HashMap<>();
        for (int i=0;i<list1.length;i++){
            map.put(list1[i],i);
        }
        int min=Integer.MAX_VALUE;
        List<String> ans=new ArrayList<>();
        for (int i=0;i<list2.length;i++){
            if (map.containsKey(list2[i])){
                if (i+map.get(list2[i])<min){
                    min=i+map.get(list2[i]);
                    ans=new ArrayList<>();
                    ans.add(list2[i]);
                }else if (i+map.get(list2[i])==min){
                    ans.add(list2[i]);
                }
            }
        }
        String[] r=new String[ans.size()];
        for (int i=0;i<ans.size();i++){
            r[i]=ans.get(i);
        }
        return r;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new Solution599().findRestaurant(new String[]{"Shogun", "Tapioca Express", "Burger King", "KFC"}, new String[]{"Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"})));
    }
}
